#include "MainHeader.h"
RectBuff TileCurs;
int InitTileset(){
	// Fill in the window class structure with parameters 
	// that describe the main window. 
	
	wcTileset.cbSize = sizeof(wcTileset);		   // size of structure 
	wcTileset.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	wcTileset.lpfnWndProc = TilesetProc;	 // points to window procedure 
	wcTileset.cbClsExtra = NULL;				// no extra class memory 
	wcTileset.cbWndExtra =NULL; 			   // no extra window memory 
	wcTileset.hInstance = hGlobal;		   // handle to instance 
	wcTileset.hIcon = NULL; 			 // predefined app. icon 
	wcTileset.hCursor = NULL;				// predefined arrow 
	// wcTileset.hbrBackground = GetStockObject( WHITE_BRUSH);				   // white background brush 
	wcTileset.lpszMenuName =  NULL;    // name of menu resource 
	wcTileset.lpszClassName = "TilesetClass";  // name of window class 
	
	
	RegisterClassEx(&wcTileset); 
	
	hwndTileset = CreateWindowEx(NULL,"TilesetClass","Tileset", WS_BORDER|WS_TABSTOP | WS_VSCROLL|WS_VISIBLE ,440,180,280,234,0,0,hGlobal,0);
	
	ShowWindow(hwndTileset,SW_SHOW);	
	return 0;
}





LRESULT CALLBACK TilesetProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	PAINTSTRUCT ps;
	HDC hdc;
	RECT coord;
	int i=0;
	GetWindowRect(hwndTileset,&coord);
	TileCurs.X = GetX(lParam);
	TileCurs.Y = GetY(lParam);
	switch (message) 
	{
		
	case WM_LBUTTONDOWN:
		//if((HWND)wParam == GetDlgItem(hwnd,picButton)) sMessage("Mouse was clicked down");
	
		if(wParam == MK_LBUTTON)	{
			//TP[0] = 0;
			//TP[1] = 0;
			// mpTileset.Width = 1;
			// mpTileset.Height = 1;
			//MessageBox(Main,"Hey, you clicked the child.","BOO",MB_OK);
			//mpTileset.Width = 1;
			//mpTileset.Height =1;
			MPToUse=1;
			Tile = (GetX(lParam)/16 ) +  ((GetY(lParam)/16)+ nVScroll[sTileset])*16;
			//memset(&IsTSA,0,sizeof(IsTSA));
			MyTSAEditor.LoadTSA();
		   
		
		}
		
		break;
	case WM_VSCROLL:
		
		UpdateScroll(hWnd,wParam,1,vsbScroll,sTileset);
		//InvalidateRect(hwndTileset,0,1);
		
		InvalidateRect(hwndTileset,0,1 );
		break;
	case WM_RBUTTONDOWN:
		if(LOWORD(wParam) == MK_RBUTTON){
		
				if(!((mpTileset.Width > 1) || (mpTileset.Height >1))){
					mpTileset.uX = mpTileset.sX = (GetX(lParam)/16);
					mpTileset.uX = mpTileset.sY = (GetY(lParam)/16);
					mpTileset.cX = mpTileset.sX;
					mpTileset.cY = mpTileset.sY;
					mpTileset.Width = 1;
					mpTileset.Height =1;
    
				}else{//Reset
					mpTileset.Width = 1;
					mpTileset.Height = 1;
				}
				InvalidateRect(hwndTileset,0,1);
		}
			
		break;
				/*case WM_RBUTTONUP:
					TP[1] = 0;
					break;*/
				case WM_MOUSEMOVE:
				
						if(wParam & (MK_RBUTTON)){
					
							mpTileset.nX = GetX(lParam)/16;
							mpTileset.nY = GetY(lParam)/16;
							//Determine direction for length and width;
							if(mpTileset.nX>mpTileset.cX){//increase width;
								mpTileset.Width+=1;
							}else if(mpTileset.nX<mpTileset.cX){
								mpTileset.Width-=1;
							}
							if(mpTileset.nY>mpTileset.cY){//increase width;
								mpTileset.Height+=1;
							}else if(mpTileset.nY<mpTileset.cY){
								mpTileset.Height-=1;
							}
							mpTileset.cX = GetX(lParam)/16;
							mpTileset.cY = GetY(lParam)/16;
							if(mpTileset.Height<1) mpTileset.Height =1;
							if(mpTileset.Width<1)mpTileset.Width = 1;
						//	if(mpTileset.Width>16)mpTileset.Width = 16;
						//	if(mpTileset.Height>16)mpTileset.Height = 16;
							//check for out of bounds 
							if(mpTileset.nX <0) mpTileset.nX = 0;
							if(mpTileset.nX>ThisLevelData->X) 
								mpTileset.nX = ThisLevelData->X;
							if(mpTileset.nY <0) mpTileset.nY = 0;
							if(mpTileset.nY>ThisLevelData->Y) mpTileset.nY = ThisLevelData->Y;
						}else{
							mpTileset.sY = GetY(lParam)/16;
							mpTileset.sX = GetX(lParam)/16;
						}
					InvalidateRect(hwndTileset,0,1);
					
					break;
					
					
				case WM_PAINT:
					
					
					
					hdc = BeginPaint(hWnd, &ps);
					GetWindowRect(hWnd,&coord);
					img[iTileset].Blit(hdc, 0,0,coord.right - coord.left ,coord.bottom-coord.top,0,nVScroll[sTileset]*16);
					DrawTileRect(hdc,mpTileset);
					
					DeleteDC(hdc);
					
					EndPaint(hWnd, &ps);
					return 0;
					break;
				case WM_SIZE:
					//Bleh this is an evil thing resizing tilesets
					DrawTileset();
					break;
				case WM_DESTROY:
					
					break;
					
					
				
	 }		
	 return DefWindowProc(hWnd, message, wParam, lParam);
	 
	 
}

